package com.zrrd.learn.chapter08面向对象.pattern.singleton.thread;

/**
 * 不安全
 *
 * @author wrn
 * @version 1.0
 * @date 2023/8/2 16:05
 */

public class BuAnQuan {

    private static int a=0;
    private synchronized static void add(){
        a++;
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                add();
            }
        });


        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                add();
            }
        });

        //计时 时间 値
        long s1 = System.currentTimeMillis();
        t1.start();
        t2.start();

//        并发数  : 高并发

        // main结束
        //捕获处理 try chatch
        try{
            t1.join();
            t2.join();
        }catch (Exception e){
            System.out.println("出错了");
        }

        System.out.println(a);
        long s2 = System.currentTimeMillis();
        //0 -20000

        System.out.println("用时==>" + (s2-s1));
    }

}


